<?php
namespace Home\Controller;
use Think\Controller;

/**
 * 联系中心
 */
class ContactCenterController extends CommnController{

    public function __construct(){
        parent::__construct();
    }

   /**
    *添加短信模板
    */
   public function add_msg(){
       if(empty($_POST)){
           $this->display();
           die;
       }
       $content=I('content');
       $add=M('user_short_message','','KEFU')->add(array('content'=>$content,'ctime'=>time(),'admin'=>$_SESSION['name']));
       if($add){
           $this->ajax('1001','添加成功');
           die;
       }
       $this->ajax('1002','添加失败');
       die;
   }

    /*
     * 客户电话沟通记录
     */
    public function tel_record_list(){
        $this->roles = role(I('rid'));
        $this->rid=I('rid');
        $this->page=I('page');
        $where['a.type'] = 2;
        $status = I('get.status');
        if (!empty($status) && $status!=4) {
            $where['a.status']=$status;
        }
        $this->status=$status;
        $search = I('get.feedback');
        $this->feedback=$search;
        if (!empty($search)) {
            $where['a.mobile|a.id|b.truename'] = array('like', '%' . $search . '%');
        }

        $feedbackModel = M('user_feedback', '', 'KEFU');
        $tol = $feedbackModel->alias('a')->join('dxd_user.user b on a.vid=b.vid')->where($where)->count('a.id');
        $row = '13';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $field='b.truename,a.ctime,a.mobile,a.reply_time,a.status,a.id';
        $data = $feedbackModel->alias('a')->join('dxd_user.user b on a.vid=b.vid')->where($where)->field($field)->order('a.id desc')->LIMIT($page->listfirst, $page->listRows)->select();
        foreach ($data as $k => $v) {
            //受理客服
            $data[$k]['admin_name'] =  M('user_message', '', 'KEFU')->where(['uf_id' => $v['id']])->getfield('admin_name');;
            //回复状态
            switch ($v['status']){
                case 1: $data[$k]['status'] = '未回复';break;
                case 2: $data[$k]['status'] = '已回复';break;
                case 3: $data[$k]['status'] = '完成';break;
                case 4: $data[$k]['status'] = '需回电';break;
            }
            if(!empty($v['ctime'])){
                $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            }
            if(!empty($v['reply_time'])){
                $data[$k]['reply_time'] = date('Y-m-d H:i', $v['reply_time']);
            }else{
                $data[$k]['reply_time']='无';
            }
        }
    
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'tol' => $tol
        ));
        $this->display();
    }
    /**
     * 修改通话记录
     */
    public function modify_tel_record($id='',$feedback,$message='',$type){//$type:1添加，2修改feedback，3修改message,4添加message
        $admin_id = $_SESSION['id'];
        M('user_feedback', '', 'KEFU')->startTrans();
        M('user_message', '', 'KEFU')->startTrans();
        if(empty($id)){
            if($type==1){
                $insert = M('user_feedback', '', 'KEFU')->add($feedback);
                if ($insert) {
                    $content='添加电话反馈-'.$insert;
                        $ret['code']=1001;
                        $ret['result']='添加成功';
                } else {
                    $ret['code']=1002;
                    $ret['result']='添加失败';
                }
            }elseif($type==4){
                $add = M('user_message', '', 'KEFU')->add($message);
                $content='添加电话反馈回复-'.$message['uf_id'];
                if ($add) {
                    $ret['code']=1001;
                    $ret['result']='添加成功';
                } else {
                    $ret['code']=1002;
                    $ret['result']='添加失败';
                }
            }

        }else{
            if($type==2){
                if(M('user_feedback', '', 'KEFU')->save($feedback)){
                    $content='修改电话反馈-'.$id;
                    $ret['code']=1001;
                    $ret['result']='修改成功';
                }else{
                    $ret['code']=1002;
                    $ret['result']='修改失败';
                }
            }elseif($type==3){
                if(M('user_message', '', 'KEFU')->save($message)){
                    $content='修改电话反馈回复-'.$id;
                    $ret['code']=1001;
                    $ret['result']='修改成功';
                }else{
                    $ret['code']=1002;
                    $ret['result']='修改失败';
                }
            }else{
                $ret['code']=1002;
                $ret['result']='修改失败';
            }
        }
        if($ret['code']==1001){
            //添加到操作日志
            $arr = array('aid' => $admin_id, 'ctime' => time(), 'content' => $content);
            $log = M('admin_log', '', 'KEFU')->add($arr);
            M('user_feedback', '', 'KEFU')->commit();
            M('user_message', '', 'KEFU')->commit();
        }else{
            M('user_feedback', '', 'KEFU')->rollback();
            M('user_message', '', 'KEFU')->rollback();
        }
        $this->ajaxreturn($ret,'json');

    }



    /**
     * 添加通话记录
     */
    public function add_tel_record(){
        if (empty($_POST)) {
            $this->name=I('name');
            $this->mobile=I('mobile');
            $this->display();
        } else {
            $phone = trim(I('phone'));
            $content = trim(I('content'));
            $time = trim(I('time'));
            $status = trim(I('status'));
            $ctme=strtotime($time);
            $reply_time=I('reply_time');
            if(!empty($reply_time)){
                $reply_time=strtotime($reply_time);
            }
            if ($phone == '' || $content == '' ) {
                $this->ajax('1002', '请填写完整');
                return false;
            }
            $vid = M('user', '', 'USER')->where(['mobile' => $phone,'truename'=>I('name')])->getfield('vid');
            if (empty($vid)) {
                $this->ajax('1002', '用户名或手机号码不对！');
                return false;
            }
            $str = array('vid' => $vid, 'content' => $content, 'ctime' =>$ctme, 'mobile' => $phone, 'back_time' => time(), 'status' => $status, 'type' => '2','reply_time'=>$reply_time);
            $this->modify_tel_record('',$str,'',1);
        }
    }

    /**
     * 电话记录详情
     */
    public function tel_record_details(){
        $id=I('id');
        $this->rid=I('rid');
        $this->feedback=I('get.feedback');
        $feedback_info = M('user_feedback', '', 'KEFU')->where(['id' => $id])->find();
        $detail_list = M('user_message', '', 'KEFU')->where(['uf_id' => $id])->select();
        foreach ($detail_list as $key => $value) {
            if(!empty($value['ctime'])){
               $detail_list[$key]['ctime'] =date('Y-m-d H:i',$value['ctime']);
            }
        }
        $name = M('user', '', 'USER')->where(['vid' => $feedback_info['vid']])->Field('truename,mobile')->find();
        if(!empty($feedback_info['ctime'])){
            $feedback_info['ctime']= date('Y-m-d H:i', $feedback_info['ctime']);
        }

        $this->admin_name=$_SESSION['name'];
        $this->id=$id;
        $this->status=I('status');
        $this->page=I('page');
        $this->assign(array(
            'feedback_info'=>$feedback_info,
            'user_name' => $name,
            'list' => $detail_list,
            'id'=>$id
        ));
        $this->display();
    }
    /**
     * 编辑电话记录
     */
    public function editfeedback($id){
        if (empty($_POST)) {
            $data=M('user_feedback', '', 'KEFU')->where(['id' => $id])->find();
            if(!empty($data['ctime'])){
                $data['ctime']=date('Y-m-d H:i',$data['ctime']);
            }
            if(!empty($data['reply_time'])){
                $data['reply_time']=date('Y-m-d H:i',$data['reply_time']);
            }
            $this->data=$data;
            $this->display();
        } else {
            $data=I('post.');
            if(!empty($data['ctime'])){
                $data['ctime']=strtotime($data['ctime']);
            }
            if(!empty($data['reply_time'])){
                $data['reply_time']=strtotime($data['reply_time']);
            }
            $this->modify_tel_record($data['id'],$data,'',2);
        }
    }
    /**
     * 编辑电话记录回复内容
     */
    public function editmessage($id){
        if (empty($_POST)) {
            $this->content=M('user_message', '', 'KEFU')->where(['id' => $id])->getfield('content');
            $this->id=$id;
            $this->display();
        } else {
            $data=I('post.');
            $this->modify_tel_record($data['id'],'',$data,3);
        }
    }



    /**
     *外呼沟通记录
     */
    public function outbound_record_list(){
        $this->roles = role(I('rid'));
        $this->rid=I('rid');
        $status = I('get.status');
        if (!empty($status)) {
            $where['a.status']=$status;
        }
        $this->status=$status;
        $search = I('get.feedback');
        $this->feedback=$search;
        if (!empty($search)) {
            $where['b.mobile|b.truename|a.content'] = array('like', '%' . $search . '%');
        }
        $model = M('user_outbound', '', 'KEFU');
        $tol = $model->where($where)->alias('a')->join('dxd_user.user b on a.vid=b.vid')->join('apart c on a.apart_id=c.id')->count('a.id');
        $row = '10';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $field='b.truename,a.ctime,a.content,a.moblie,a.status,a.id,c.name as apart_name';
        $data = $model->alias('a')->join('dxd_user.user b on a.vid=b.vid')->join('apart c on a.apart_id=c.id')->where($where)->field($field)->order('a.id desc')->LIMIT($page->listfirst, $page->listRows)->select();
        foreach ($data as $k => $v) {
            switch ($v['status']){
                case 1: $data[$k]['status'] = '未沟通';break;
                case 2: $data[$k]['status'] = '已沟通';break;
                case 3: $data[$k]['status'] = '已完成';break;
            }
            if(!empty($v['ctime'])){
                $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            }
            if(!empty($v['reply_time'])){
                $data[$k]['reply_time'] = date('Y-m-d H:i', $v['reply_time']);
            }else{
                $data[$k]['reply_time']='无';
            }
        }
        $this->page=I('page');
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'tol' => $tol
        ));
        $this->display();
    }
    /**
     * 创建外呼沟通记录
     */
    public function add_outbound_record(){
        if (empty($_POST)) {
            $this->name=I('name');
            $this->mobile=I('mobile');
            $this->apart=M('apart', '', 'KEFU')->select();
            $this->display();
        } else {
            $data=I('post.');
            $data['vid'] = M('user', '', 'USER')->where(['mobile' => $data['moblie'],'truename'=>$data['name']])->getfield('vid');
            unset($data['name']);
            if (empty($data['vid'])) {
                $this->ajax('1002', '用户号码不对！');
                return false;
            }
            $data['ctime']=time();
           if( $id=M('user_outbound', '', 'KEFU')->add($data)){
               $arr = array('aid' => $_SESSION['id'], 'ctime' => time(), 'content' => '添加外呼沟通记录-'.$id);
               $log = M('admin_log', '', 'KEFU')->add($arr);
               $this->ajaxreturn(['code'=>1001,'result'=>'添加成功'],'json');
           }else{
               $this->ajaxreturn(['code'=>1002,'result'=>'添加失败'],'json');
           }
        }
    }
    /**
     * 外呼沟通记录详情
     */
    public function outbound_record_details($id){
        $this->rid=I('rid');
        $this->feedback=I('get.feedback');
        $field='b.truename,a.ctime,a.content,a.moblie,a.status,a.id,c.name as apart_name,b.vid';
        $user_outbound=M('user_outbound', '', 'KEFU')->alias('a')->join('dxd_user.user b on a.vid=b.vid')->join('apart c on a.apart_id=c.id')->field($field)->where(['a.id'=>$id])->find();
        $detail_list = M('user_outbound_message', '', 'KEFU')->where(['o_id' => $id])->select();
        if(!empty($user_outbound['ctime'])){
            $user_outbound['ctime'] = date('Y-m-d H:i', $user_outbound['ctime']);
        }
        foreach ($detail_list as $k=>$v){
            if(!empty($v['ctime'])){
                $detail_list[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            }
            if(!empty($v['reply_time'])){
                $detail_list[$k]['reply_time'] = date('Y-m-d H:i', $v['reply_time']);
            }
        }
        $this->user_outbound=$user_outbound;
        $this->detail_list=$detail_list;
        $this->admin_name=$_SESSION['name'];
        $this->page=I('page');
        $this->status=I('status');
        $this->display();
    }

    /**
     * 修改回复
     */
     public function modify_outbound($data,$type){//$type:1添加，2修改
         if($type==1){
             M('user_outbound_message', '', 'KEFU')->startTrans();
             M('user_outbound', '', 'KEFU')->startTrans();
             $status=M('user_outbound', '', 'KEFU')->where(['id'=>$data['o_id']])->getfield('status');
             if($status==1){
                 if(!M('user_outbound', '', 'KEFU')->where(['id'=>$data['o_id']])->save(['status'=>2])){
                     M('user_outbound', '', 'KEFU')->rollback();
                     M('user_outbound_message', '', 'KEFU')->rollback();
                     $ret['code']=1002;
                     $ret['result']='回复失败';
                     $this->ajaxreturn($ret,'json');
                 }
             }
             if($id=M('user_outbound_message', '', 'KEFU')->add($data)){
                 $content='添加外呼沟通记录回复-'.$id;
                 $ret['code']=1001;
                 $ret['result']='回复成功';
                 M('user_outbound', '', 'KEFU')->commit();
                 M('user_outbound_message', '', 'KEFU')->commit();
             } else {
                 $ret['code']=1002;
                 $ret['result']='添加失败';
                 M('user_outbound', '', 'KEFU')->rollback();
                 M('user_outbound_message', '', 'KEFU')->rollback();
             }
         }elseif($type==2){
             if(M('user_outbound_message', '', 'KEFU')->save($data)){
                 $content='修改外呼沟通记录回复-'.$data['id'];
                 $ret['code']=1001;
                 $ret['result']='修改成功';
             } else {
                 $ret['code']=1002;
                 $ret['result']='修改失败';

             }
         }else{
             $ret['code']=1002;
             $ret['result']='修改失败';
         }
         if($ret['code']==1001){
             //添加到操作日志
             $arr = array('aid' => $_SESSION['id'], 'ctime' => time(), 'content' => $content);
             $log = M('admin_log', '', 'KEFU')->add($arr);
         }
         $this->ajaxreturn($ret,'json');
     }
    /**
     * 添加回复
     */
    public function add_outbound(){
        $data=I('post.');
        $data['admin_name']=$_SESSION['name'];
        $data['ctime']=time();
        $this->modify_outbound($data,1);
    }
    /**
     * 编辑回复
     */
    public function edit_outbound(){
        if (empty($_POST)) {
            $this->id=I('id');
            $data= M('user_outbound_message', '', 'KEFU')->where(['id'=>I('id')])->field('content')->find();
            $this->data=$data;
            $this->display();
        }else{
            $data=I('post.');
            $this->modify_outbound($data,2);
        }
    }

    public function modify_outbound_status($id,$status){
        if(M('user_outbound', '', 'KEFU')->where(['id'=>$id])->save(['status'=>$status])){
            $ret['code']=1001;
            $ret['result']='修改成功';
        }else{
            $ret['code']=1002;
            $ret['result']='修改失败';
        }
        return $ret;
    }

    /**
     * 完成
     */
    public function save_outbound(){
        $ret=$this->modify_outbound_status(I('id'),3);
        $this->ajaxreturn($ret,'json');
    }

    //反馈列表
    public function kf_user_list_feedback(){
        $this->roles = role(I('rid'));
        $this->rid=I('rid');
        $this->page=I('page');
        $feedbackModel = M('user_feedback', '', 'KEFU');
        $search = I('get.feedback');
        $this->feedback=$search;
        $status=I('get.status');
        $this->status=$status;
        $where['b.type']=1;
        $where1['type']=1;
        if (!empty($search)) {
            $where['b.content|b.mobile|b.id|a.truename|b.ctime'] = array('like', '%' . $search . '%');
        }
        if(!empty($status)){
            $where['b.status'] = $status;
            $where1['status'] = $status;
        }
        $tol = $feedbackModel->where($where1)->count();
        $row = '10';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $field = 'b.content,b.id,b.mobile,b.ctime,b.status,b.type,b.upd_time,b.vid,a.truename as username';
        $data = $feedbackModel->join('b join dxd_user.user a on b.mobile=a.mobile')->where($where)->LIMIT($page->listfirst, $page->listRows)->field($field)->order('b.ctime desc')->select();


        foreach ($data as $k => $v) {
           /* $name = M('user', '', 'USER')->where(['vid' => $v['vid']])->field('truename')->find();
            $data[$k]['username'] = $name['truename'];*/
            //回复状态
            switch ($v['status']){
                case 1:$data[$k]['status'] = '未回复';break;
                case 2:$data[$k]['status'] = '已回复';break;
                case 3:$data[$k]['status'] = '完成';break;
            }

            if(strpos($v['content'],'/')!==false || strstr($v['content'],'>')){
                $data[$k]['content']=substr($v['content'],0,5);
            }

            //回复方式
            if ($v['type'] == '1') {
                $data[$k]['type'] = '短信反馈';
            } else if ($v['type'] == '2') {
                $data[$k]['type'] = '电话反馈';
            }
            $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            $data[$k]['back_time'] = date('Y-m-d H:i', $v['upd_time']);
        }

        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'tol' => $tol
        ));
        $this->display();


    }

    //反馈详情
    public function kf_feedback_detail(){
        $this->type2=I('type2');
        $this->user_content=I('user_content');
        $this->rid=I('rid');
        $id = I('id');
        $this->vid=I('vid');
         $anti_type=I('anti_type');
         if($anti_type=='1'){
             $feedback_info = M('user_feedback', '', 'KEFU')->where(['vid' => $this->vid])->order('id desc')->find();
         }else{
             $feedback_info = M('user_feedback', '', 'KEFU')->where(['id' => $id])->find();
         }


        if(strpos($feedback_info['content'],'/')!==false || strstr($feedback_info['content'],'>')){
            $feedback_info['content']=substr($feedback_info['content'],0,5);
        }
//        $feedback_info = M('user_feedback', '', 'KEFU')->where(['id' => $id])->find();
        $name = M('user', '', 'USER')->where(['vid' => $feedback_info['vid']])->Field('truename,mobile,id')->find();
        $detail_list = M('user_message', '', 'KEFU')->where(['uf_id' => $feedback_info['id']])->select();
        foreach ($detail_list as $k => $v) {
            $detail_list[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            if (!empty($v['img'])) {
                $detail_list[$k]['img'] = explode(',', $v['img']);
            }
        }
        if (!empty($feedback_info['img'])){
            $imgs = explode(',', $feedback_info['img']);
        } else {
            $imgs = '';
        }
        if (!empty($feedback_info['ctime'])) {
            $feedback_info['ctime'] = date('Y-m-d H:i', $feedback_info['ctime']);
        }

        $admin=M('admin','','KEFU')->where(['id'=>$_SESSION['id']])->field('agent_status,status')->find();
        $show='2';
        if($admin['agent_status']==1 && $admin['status']==1  ){
            $show='1';
        }

        $this->assign(array(
            'name' => $name,
            'feedback_info' => $feedback_info,
            'imgs1' => $imgs,
            'id' => $id,
            'show' => $show,
            'role'=> $_SESSION['role_id'],
            'user_id' => $name['id'],
            'list' => $detail_list,
            'anti_type' => $anti_type,
        ));
        $this->status = I('status');
        $this->feedback = I('feedback');
        $this->page = I('page');
        $this->display();
    }

    /**
     * 电话反馈
     */
    public function phone_record(){
        $admin_id = $_SESSION['id'];
        if (empty($_POST)) {
            $this->display();
        } else {
            $phone = trim(I('phone'));
            $content = trim(I('content'));
            $back_content = trim(I('back_content'));
            $time = trim(I('time'));
            $status = trim(I('status'));
            $ctme=strtotime($time);
            if ($phone == '' || $content == '' || $back_content == '') {
                $this->ajax('1002', '请填写完整');
                return false;
            }

            $vid = M('user', '', 'USER')->where(['mobile' => $phone])->getfield('vid');
            if (empty($vid)) {
                $this->ajax('1002', '用户账户号码不对！');
                return false;
            }
            $str = array('vid' => $vid, 'content' => $content, 'ctime' =>$ctme, 'mobile' => $phone, 'back_time' => time(), 'status' => $status, 'type' => '2');

            $insert = M('user_feedback', '', 'KEFU')->add($str);
            if ($insert) {
                $arr = array('vid' => '0', 'content' => $back_content, 'ctime' => $ctme, 'uf_id' => $insert, 'img' => '','admin_name'=>$_SESSION['name']);
                $add = M('user_message', '', 'KEFU')->add($arr);

                //添加到操作日志
                $arr = array('aid' => $admin_id, 'ctime' => time(), 'content' => '添加电话反馈-'.$insert);
                $log = M('admin_log', '', 'KEFU')->add($arr);

                if ($add) {
                    $this->ajax('1001', '添加成功');
                } else {
                    $this->ajax('1002', '添加失败');
                }
            } else {
                $this->ajax('1002', '添加失败');
            }
        }
    }

    /**
     * 与 电话回复详情
     */
    public function phone_feedback_details(){

        $id=I('id');
        $content=I('back_content');
        $status=I('status');

        $data=[];
        $data['status']=$status;
        $save = M('user_feedback', '', 'KEFU')->where(['id'=>$id])->save($data);

        $arr = array('vid' => '0', 'content' => $content, 'ctime' => time(), 'uf_id' => $id, 'img' => '','admin_name'=>$_SESSION['name']);
        $add = M('user_message', '', 'KEFU')->add($arr);


        if ($add) {
            //添加到操作日志
            $arr = array('aid' => $_SESSION['id'], 'ctime' => time(), 'content' => '回复电话反馈-'.$id);
            $log = M('admin_log', '', 'KEFU')->add($arr);
            $this->ajax('1001', '添加成功');
        } else {
            $this->ajax('1002', '添加失败');
        }



    }


    public function ajax($code, $result, $r = '')
    {
        if (empty($r)) {
            $this->ajaxReturn(['code' => $code, 'result' => $result]);
        } else {
            $this->ajaxReturn(['code' => $code, 'result' => $result]);
            return false;
        }
    }
}
?>
